/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkeddata.arida.ufc.br.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import linkeddata.arida.ufc.br.manager.impl.BuildsHierarchyDataProperty;
import linkeddata.arida.ufc.br.util.OWLUtils;
import org.semanticweb.owlapi.model.OWLEntity;

/**
 *
 * @author jeovane
 */
public class MenuHierarchyOntologyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String domainOntology = request.getParameter("addressDom");
        
        OWLUtils utils = new OWLUtils(domainOntology);
        utils.ontologyLoaded();
        BuildsHierarchyDataProperty bhdp = new BuildsHierarchyDataProperty(utils.getOntology(), utils.getReasoner(), utils.getPm());
        
        Map<String, ArrayList<String>> mapClass = new TreeMap<String, ArrayList<String>>();
        ArrayList<String> values;
        
        for (Map.Entry entryBH : bhdp.buildsHierarchy().entrySet()) {
            String key = utils.getPm().getShortForm((OWLEntity) entryBH.getKey()).replace(":", "");
            ArrayList<OWLEntity> arrayValues = (ArrayList<OWLEntity>) entryBH.getValue();
            values = new ArrayList<String>();
            for (OWLEntity owlEntity : arrayValues) {
                String valueKey = utils.getPm().getShortForm(owlEntity).replace(":", "");
                values.add(valueKey);
            }
            mapClass.put(key, values);
        }

        // Mando o Map<K,V> para próxima página
        request.setAttribute("mapClass", mapClass);
        request.setAttribute("domainOntology", domainOntology);

        RequestDispatcher rd = request.getRequestDispatcher("/page-loaded-ontology.jsp");
        rd.forward(request, response);
    }
}
